package cn.fuxue.hrms.service.impl;

import cn.fuxue.hrms.common.dto.CvInfoShow;
import cn.fuxue.hrms.common.entity.Deleted;
import cn.fuxue.hrms.common.entity.Role;
import cn.fuxue.hrms.mapper.PermissionsRoleMapper;
import cn.fuxue.hrms.service.PermissionsRoleService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

@Service

public class PermissionsRoleServiceImpl implements PermissionsRoleService {
    /*private static final Logger logger= (Logger) LoggerFactory.getLogger(PermissionsRoleServiceImpl.class);*/
    @Autowired
    private PermissionsRoleMapper permissionsRoleMapper;
    @Override
    public List<Role> selectAllRole() {
        //查询所有的角色信息
        List<Role> roleList= permissionsRoleMapper.selectAllRole();

        for (Role role : roleList) {
            String roleId = String.valueOf(role.getRoleId());
            String roleName = role.getRoleName();
            Integer permissionCategory = role.getPermissionCategory();
            Integer createId = role.getCreateId();
            Integer modifyId = role.getModifyId();
            Date createTime = role.getCreateTime();
        }

        return roleList;
    }



    @Override
    public int insertRole(Role role) {
        try {
            // 检查角色ID是否重复
            List<Role> existingRoles = permissionsRoleMapper.selectAllRole();
            boolean exists = existingRoles.stream()
                    .anyMatch(r -> r.getRoleId().equals(role.getRoleId()));

            if(exists) {
                /*logger.warning("角色ID已存在: {}");*/
                throw new RuntimeException("角色ID已存在");
            }

            // 设置默认值
            role.setIsDeleted(Deleted.Delete); // 逻辑删除标识默认未删除
            return permissionsRoleMapper.insertRole(role);
        } catch (Exception e) {
            /*logger.log(Level.parse("添加角色失败: {}"), e.getMessage());*/
            throw new RuntimeException("添加角色失败", e);
        }
    }

    @Override
    public int deleteRoleById(Integer Id) {
        return permissionsRoleMapper.deleteRoleById(Id);
    }


}
